<?php

class LoginView {

	private $m_username = "username";
	private $m_password = "password";
	private $m_logout = "logout";
	
	
	//konstanter som kan anropas i LoginController
	const LOGGEDIN = 0;
	const FORMATFAIL = 1;
	const  LOGGEDOUT = 2;
	
	
	// Genom GET kan man anropa formuläret i andra funktioner
	public function DoLoginBox() {

		$loginForm = "
		<form method='POST'>
  		Username: <input type='text' name='" . $this -> m_username . "' size='15' /><br />
  		Password: <input type='password' name='" . $this -> m_password . "'  size='15' /><br />
  		<p><input type='submit' value='Login'/></p>
  		</form>";
		return $loginForm;
	}
	
	public function DoLogoutBox() {
		$logoutButton = "<form method='POST'>
		<input type='hidden' value='active' name='" . $this -> m_logout . "'/>
		<input type='submit' value='Logout'/></form>";
		return $logoutButton;
	}
	
	public function LoggedInAs($username){
		
		$loggedInAs = "<div id='loggedIn'>Inloggad som" + $username + "</div>";
		return $loggedInAs;
	}
	
	public static function statusMessage($n){
				$statusMessage = null;
				
				switch($n){
					case self:: LOGGEDIN:
						$statusMessage .= "Du är inloggad.";
						break;
					case self:: FORMATFAIL:
						$statusMessage .= "Ange kontouppgifter med siffror eller bokstäver. Minst 6 tecken och högst 12.";
						break;
					case self:: LOGGEDOUT:
						$statusMessage .= "Du är utloggad.";
						break;
					
					}
		return "<p class='statusMessage'> $statusMessage</p>";
	}

	public function GetUserName() {
		if (isset($_POST[$this -> m_username]) == true) {
			//return the variable
			return $_POST[$this -> m_username];
		} else if(isset($_COOKIE[$this->m_username])) {
			return $_COOKIE[$this->m_username];
		}
		return NULL;
	}

	public function GetPassword() {
		if (isset($_POST[$this -> m_password]) == true) {
			//return the variable
			return $_POST[$this -> m_password];
		} else if(isset($_COOKIE[$this->m_password])) {
			return $_COOKIE[$this->m_password];
			}
		return NULL;
	}

	// Så länge värdena som mottas inte är null så får man inte felmedellande
	public function TriedToLogin() {
		if (($this -> GetUserName() != NULL) && ($this -> GetPassword() != NULL)) {
			return true;
		} else {
			return false;
		}
	}

	// anropar elementen för knappen och via isset kollar man om den trycks på
	public function TriedToLogout() {
		if (isset($_POST[$this -> m_logout]) == true) {
			return true;
		} else {
			return false;
		}
	}
	//Settar cookies genom att anropa värdena som sparas i username och password, sätter en tid på 1h
	public function setCookie(){
			setcookie($this->m_username, $this->GetUserName(),  time()+3600);
			$_COOKIE[$this->m_username] = $this->GetUserName();
			setcookie($this->m_password, $this->GetPassword(),  time()+3600);
				$_COOKIE[$this->m_password] = $this->GetPassword();
			
		
		
	}
	//Returnerar cookien som lagras i variablen m_remember genom kryssrutan
	public function rememberCookie(){
		return isset($_POST[$this -> m_username]);
	
	}
	
	//Unsettar cookies
	public function unsetCookie(){
		
		setcookie($this->m_username, $this->GetUserName(), time() -36001);
		setcookie($this->m_password, $this->GetPassword(), time() -36001);
		
		unset($_COOKIE[$this->m_username]);
		unset($_COOKIE[$this->m_password]);
		
	}
	//Har cookien ett värde returneras true.
	public function readCookie()
	{
		if(isset($_COOKIE[$this->m_username]) && isset($_COOKIE[$this->m_password]))
		{
			return true;
		}
		return false;
	}

}
?>
